package cn.piaoyi.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.piaoyi.bean.Address;
import cn.piaoyi.bean.Admin;
import cn.piaoyi.bean.Category;
import cn.piaoyi.bean.City;
import cn.piaoyi.bean.Goods;
import cn.piaoyi.bean.Province;
import cn.piaoyi.bean.UserInfo;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.comm.Util;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.AddressService;
import cn.piaoyi.service.AdminService;
import cn.piaoyi.service.CategoryService;
import cn.piaoyi.service.CityService;
import cn.piaoyi.service.GoodsService;
import cn.piaoyi.service.PosterService;
import cn.piaoyi.service.UserInfoService;
import cn.piaoyi.service.web.PageList;
import cn.piaoyi.service.web.Pages;
import cn.piaoyi.web.interceptor.RemoteAddrAware;
import cn.piaoyi.web.interceptor.UserSessionAware;
import cn.piaoyi.web.servlet.AdminSession;
import cn.piaoyi.web.servlet.UserSession;
import cn.piaoyi.web.ui.OptionsInt;
import cn.piaoyi.web.ui.OptionsLong;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

public class UserAddress extends BaseAction implements SessionAware,UserSessionAware{
	private static final Log logger = LogFactory.getLog(UserAddress.class);
	
	private AddressService addressService;
	private CityService cityService;

	public AddressService getAddressService() {
		return addressService;
	}

	public void setAddressService(AddressService addressService) {
		this.addressService = addressService;
	}

	public CityService getCityService() {
		return cityService;
	}

	public void setCityService(CityService cityService) {
		this.cityService = cityService;
	}
	
	private List provinceList = new ArrayList();
	private List cityList = new ArrayList();

	public List getProvinceList() {
		return provinceList;
	}

	public void setProvinceList(List provinceList) {
		this.provinceList = provinceList;
	}

	public List getCityList() {
		return cityList;
	}

	public void setCityList(List cityList) {
		this.cityList = cityList;
	}
	
	private void setProvinceListValues(){
		List l = this.getCityService().findProvinceAll();
		for(int i=0;i<l.size();i++){
			Province p = (Province) l.get(i);
			provinceList.add(new OptionsLong(p.getId(), p.getName()));
		}
	}
	
	private void setCityListValues(long pid){
		Province p = this.getCityService().findProvinceById(pid);
		List l = this.getCityService().findCitysByProvinceId(p.getId());
		for(int i=0;i<l.size();i++){
			City c = (City) l.get(i);
			cityList.add(new OptionsLong(c.getId(), c.getName()));
		}
	}
	
	private void initValues(long pid){
		setProvinceListValues();
		setCityListValues(pid);
	}
	
	private UserSession userSession;
	private Map session;

	public UserSession getUserSession() {
		return userSession;
	}

	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}
	
	public Map getSession() {
		return session;
	}
	
	public void setSession(Map session) {
		this.session = session;
	}
	
	
	private String consignee;	//收件人
	private int province = 0;	//省
	private int city = 0;		//市
	private String district;	//地区
	private String address;		//地址
	private String zipcode;		//邮编
	private String mobile;		//手机
	private String email;		//邮箱

	public String getConsignee() {
		return consignee;
	}

	public void setConsignee(String consignee) {
		this.consignee = consignee;
	}
	
	public int getProvince() {
		return province;
	}

	public void setProvince(int province) {
		this.province = province;
	}

	public int getCity() {
		return city;
	}

	public void setCity(int city) {
		this.city = city;
	}

	public String getDistrict() {
		return district;
	}
	
	public void setDistrict(String district) {
		this.district = district;
	}
	
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getZipcode() {
		return zipcode;
	}

	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	private List citys;

	public List getCitys() {
		return citys;
	}

	public void setCitys(List citys) {
		this.citys = citys;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		Address a = this.getAddressService().finAddressByUId(this.getUserSession().getUid());
		this.setConsignee(a.getConsignee());
		this.setProvince(a.getProvince());
		this.setCity(a.getCity());
		this.setDistrict(a.getDistrict());
		this.setAddress(a.getAddress());
		this.setZipcode(a.getZipcode());
		this.setMobile(a.getMobile());
		this.setEmail(a.getEmail());
		this.setAction("save");
		if(this.getProvince()==0){
			List l = this.getCityService().findProvinceAll();
			Province p = (Province) l.get(0);
			initValues(p.getId());
		}else{
			initValues(this.getProvince());
		}
		this.setCitys(this.getCityService().findCityAll());
		return SUCCESS;
	}
	
	/**
	 * 保存
	 */
	public String save(){
		Address a = this.getAddressService().finAddressByUId(this.getUserSession().getUid());
		a.setConsignee(this.getConsignee());
		a.setProvince(this.getProvince());
		a.setCity(this.getCity());
		a.setDistrict(this.getDistrict());
		a.setAddress(this.getAddress());
		a.setZipcode(this.getZipcode());
		a.setMobile(this.getMobile());
		a.setEmail(this.getEmail());
		try {
			this.getAddressService().updateAddress(a);
		} catch (PiaoyiException e) {
            this.addActionError(this.getText("error.address.updaterror"));
            return ERROR;
        }
		if(this.getProvince()==0){
			List l = this.getCityService().findProvinceAll();
			Province p = (Province) l.get(0);
			initValues(p.getId());
		}else{
			initValues(this.getProvince());
		}
		this.setCitys(this.getCityService().findCityAll());
		return SUCCESS;
	}
}
